package cn.ex.ymcc.web.controller;

import cn.ex.ymcc.domain.CourseUserLearn;
import cn.ex.ymcc.service.ICourseService;
import cn.ex.ymcc.domain.Course;
import cn.ex.ymcc.query.CourseQuery;
import cn.ex.ymcc.result.JSONResult;
import cn.ex.ymcc.result.PageList;
import cn.ex.ymcc.service.ICourseUserLearnService;
import cn.ex.ymcc.vo.CourseDetailInfoVo;
import cn.ex.ymcc.vo.CourserOrderVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    @Autowired
    private ICourseUserLearnService courseUserLearnService;


    /**
     * 根据课程Id查询订单结算页需要的课程相关数据
     * @param courseIds 一个或者多个课程Id，如果是多个用“,”分割
     * @return
     */
    @RequestMapping(value="/info/{courseIds}",method= RequestMethod.GET)
    public JSONResult courseOrderInfo(@PathVariable("courseIds") String courseIds){
        CourserOrderVo vo = courseService.courserOrderVo(courseIds);
        return JSONResult.success(vo);
    }

    /**
     * 查询课程详情
     * @param courseId 课程Id
     * @return  课程详情页需要的数据
     */
    @RequestMapping(value="/detail/data/{courseId}",method= RequestMethod.GET)
    public JSONResult searchCourseDetail(@PathVariable("courseId") Long courseId){
        CourseDetailInfoVo vo = courseService.searchCourseDetail(courseId);
        return JSONResult.success(vo);
    }




    /**
     * 课程发布
     */
    @RequestMapping(value="/onLineCourse/{id}",method= RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("id")Long id){
        courseService.onLineCourse(id);
        return JSONResult.success();
    }

    /**
     * 查看用户的课程
     */
    @RequestMapping(value = "/listForUser",method = RequestMethod.GET)
    public JSONResult listForUser(){
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",1L);
        List<CourseUserLearn> courseUserLearns = courseUserLearnService.selectList(wrapper);
        //查询课程
        List<Course> courses = courseService.selectBatchIds(courseUserLearns.stream().map(c -> c.getCourseId()).collect(Collectors.toList()));
        return JSONResult.success(courses);
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
